﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;

using ValuationModel;
using ValuationBLL;
using Newtonsoft.Json;
using System.Web;
using System.IO;

namespace ValuationServers.Controllers
{
    public class ProjectController : ApiController
    {
        ProjectBLL bll = new ProjectBLL();

        #region  项目立项
        /// <summary>
        /// 立项显示
        /// </summary>
        /// <returns></returns>
        public IEnumerable<EABasic_Info> ProjectApprovalList()
        {
            return bll.ProjectApprovalList();
        }
        /// <summary>
        /// 立项显示及勘察显示及评估显示
        /// </summary>
        /// <returns></returns>
        [HttpGet]
        public string GetBasicInfo(int id, string name)
        {
            var list = from a in bll.GetList<EABasic_Info>()
                       join b in bll.GetList<EALoan_Info>()
                       on a.projectNumber equals b.projectNumber
                       join c in bll.GetList<SysDepartment>()
                       on a.the_Department equals c.Department_Id
                       select new
                       {
                           a.basic_id,
                           a.surplus_Years,
                           a.basic_Quota,
                           a.house_Located,
                           a.house_Purpose,
                           a.house_Time,
                           a.yers_Source,
                           a.assessment,
                           a.presentation_Type,
                           a.the_Department,
                           a.survey,
                           a.evaluation,
                           a.basic_State,
                           a.RegisterTime,
                           a.issue_Time,
                           a.survey_StartTime,
                           a.survey_EndTime,
                           a.basic_ValueTime,
                           b.loan_Name,
                           b.loan_IdCard,
                           b.loan_TEL,
                           b.loan_ContactsTEL,
                           b.loan_Contacts,
                           //b.createTime,
                           b.projectNumber,
                           c.Department_Name

                       };

            // return JsonConvert.SerializeObject(list);
            if (id == 0)
            {
                return JsonConvert.SerializeObject(list.ToList());
            }
            else if (id == 1)
            {
                return JsonConvert.SerializeObject(list.Where(t => t.Department_Name.Contains(name)).ToList());
            }
            else if (id == 2)
            {
                return JsonConvert.SerializeObject(list.Where(t => t.projectNumber.Contains(name)).ToList());
            }
            else
                if (id == 3)
                {
                    return JsonConvert.SerializeObject(list.Where(t => t.loan_Name.Contains(name)).ToList());
                }
                else
                {
                    return JsonConvert.SerializeObject(list.Where(t => t.house_Located.Contains(name)).ToList());
                }
        }
        /// <summary>
        ///显示下拉框
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        [HttpGet]
        public string SelectBasic()
        {
            var list = from a in bll.GetList<EABasic_Info>()
                       join b in bll.GetList<EALoan_Info>()
                       on a.projectNumber equals b.projectNumber
                       join c in bll.GetList<SysDepartment>()
                       on a.the_Department equals c.Department_Id
                       select new
                       {
                           a.basic_id,
                           a.surplus_Years,
                           a.basic_Quota,
                           a.house_Located,
                           a.house_Purpose,
                           a.house_Time,
                           a.yers_Source,
                           a.assessment,
                           a.presentation_Type,
                           a.the_Department,
                           a.survey,
                           a.evaluation,
                           a.basic_State,
                           a.RegisterTime,
                           a.issue_Time,
                           a.survey_StartTime,
                           a.survey_EndTime,
                           a.basic_ValueTime,
                           b.loan_Name,
                           b.loan_IdCard,
                           b.loan_TEL,
                           b.loan_ContactsTEL,
                           b.loan_Contacts,
                           //b.createTime,
                           b.projectNumber,
                           c.Department_Name,
                           c.Department_Id
                       };
            return JsonConvert.SerializeObject(list.ToList().Distinct());
        }
        /// <summary>
        /// 登记信息
        /// </summary>
        /// <param name="Basic"></param>
        /// <param name="name"></param>
        /// <param name="card"></param>
        /// <param name="tel"></param>
        /// <param name="userName"></param>
        /// <param name="userTel"></param>
        /// <returns></returns>
        public int Post(EABasic_Info Basic, string name, string card, string tel, string userName, string userTel)
        {
            EALoan_Info alf = new EALoan_Info();  //添加借款人信息
            alf.loan_Name = name;
            alf.loan_IdCard = card;
            alf.loan_TEL = tel;
            alf.loan_Contacts = userName;
            alf.loan_ContactsTEL = userTel;

            var Jid = bll.List().FirstOrDefault().loan_Id + 1;  //获取到当前借款人的ID
            //拼接成项目编号
            int number = Convert.ToInt32(DateTime.Now.Year + "0000" + Jid);
            alf.projectNumber = number.ToString();

            Basic.projectNumber = number.ToString();
            if (bll.AddEntity(Basic) > 0) //登记
            {
                bll.AddLoan(alf);//添加借款人
                return 1;
            }


            return 0;
        }


        //详情显示

        [HttpGet]
        public  DengJi BackData(string pnum)
        {
            var result = bll.BackDatas(pnum);
            return result;
        }
        //询价添加

        public bool InsertEAInquiry(EAInquiry eAInquiry)
        {
            eAInquiry.inquiry_Time=DateTime.Now;
            return bll.InsertEAInquiry<EAInquiry>(eAInquiry);
        }
        //询价显示
        [HttpGet]
        public List<EAInquiry> EAInquiryList()
        {
            return bll.EAInquiryList();
        }


        #endregion

        #region 项目审核
        #region 评估

        //我的评估
        [HttpGet]
        public string MyEstimate(int id, string name, string username)
        {
            //使用linq查询借款信息
            var list = from a in bll.GetList<EABasic_Info>()
                       join b in bll.GetList<EALoan_Info>()
                       on a.projectNumber equals b.projectNumber
                       select new
                       {
                           a.basic_id,
                           a.surplus_Years,
                           a.basic_Quota,
                           a.house_Located,
                           a.house_Purpose,
                           a.house_Time,
                           a.yers_Source,
                           a.assessment,
                           a.presentation_Type,
                           a.the_Department,
                           a.survey,
                           a.evaluation,
                           a.basic_State,
                           //a.basic_CreateTime,
                           //a.basic_CreatePeople,
                           a.issue_Time,
                           a.survey_StartTime,
                           a.survey_EndTime,
                           a.basic_ValueTime,
                           b.loan_Name,
                           b.loan_IdCard,
                           b.loan_TEL,
                           b.loan_ContactsTEL,
                           b.loan_Contacts,
                           //b.createTime,
                           b.projectNumber,
                           a.EstimatePeople
                       };
            if (id == 0)//当id为0时查询出所有的信息
            {
                return JsonConvert.SerializeObject(list.Where(t => t.EstimatePeople == username & t.basic_State >= 3).ToList());
            }
            else if (id == 1)//通过部门查询借款人信息
            {
                return JsonConvert.SerializeObject(list.Where(t => t.the_Department == Convert.ToInt32(name) & t.basic_State >= 3 & t.EstimatePeople == username).ToList());
            }
            else if (id == 2)//通过项目编号查询借款人信息
            {
                return JsonConvert.SerializeObject(list.Where(t => t.basic_id == Convert.ToInt32(name) & t.basic_State >= 3 & t.EstimatePeople == username).ToList());
            }
            else
                if (id == 3)//通过借款人查询借款人信息
                {
                    return JsonConvert.SerializeObject(list.Where(t => t.loan_Name.Contains(name) & t.basic_State >= 3 & t.EstimatePeople == username).ToList());
                }
                else//通过房屋坐落查询借款人信息
                {
                    return JsonConvert.SerializeObject(list.Where(t => t.house_Located.Contains(name) & t.basic_State >= 3 & t.EstimatePeople == username).ToList());
                }
        }

        //显示所有的评估订单
        [HttpGet]
        public string ShowEstimate()
        {
            return JsonConvert.SerializeObject(bll.GetList<EABasic_Info>().Where(t => t.basic_State == 2));//状态为待评估
        }

        //评估受理
        //[HttpPut]
        //public bool UpdateEstimate(string number, string username)
        //{
        //    EABasic_Info basic =bll.GetList<EABasic_Info>().Where(t => t.projectNumber == number).FirstOrDefault();
        //    basic.basic_State = 9;//修改项目状态
        //    basic.EstimatePeople = username;
        //    if (bll.(basic, basic.basic_id))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }

        //}

        //评估结束
        //[HttpPut]
        //public bool UpdateEstimate(string number, int basic_Evaluation, DateTime basic_ValueTime, decimal basic_UnitPrice, double basic_Acreage, decimal basic_Total);
        //{
        //    EABasic_Info basic = bll.GetList<EABasic_Info>().Where(t => t.projectNumber == number).FirstOrDefault();
        //    basic.basic_State = 3;//修改项目状态
        //    basic.basic_Evaluation = basic_Evaluation;
        //    basic.basic_ValueTime = basic_ValueTime;
        //    basic.basic_UnitPrice = basic_UnitPrice;
        //    basic.basic_Acreage =Convert.ToDecimal(basic_Acreage);
        //    basic.basic_Total = basic_Total;
        //    if (bll.Update(basic, basic.basic_id))
        //    {
        //        return true;
        //    }
        //    else
        //    {
        //        return false;
        //    }

        //}

        //文件上传
        [HttpPut]
        public int Files(int i)
        {
            //通过HttpContext对象进行文件传输，将传输到的文件流赋给HttpFileCollection对象
            HttpFileCollection httpfilecollection = HttpContext.Current.Request.Files;

            //判断文件流的长度是否大于0
            if (httpfilecollection.Count > 0)
            {
                //将读取到的文件流的数据读取到并赋值给HttpFileBase对象
                HttpPostedFile hp = httpfilecollection[0];

                //获取文件的扩展名判断文件是否合法
                string fileextten = Path.GetExtension(hp.FileName);

                if (fileextten.Contains(".xlsx") || fileextten.Contains(".xlx") || fileextten.Contains(".zip") || fileextten.Contains(".rar"))
                {
                    //将文件存储到本地路径下
                    hp.SaveAs(HttpContext.Current.Server.MapPath("/File/") + hp.FileName);

                    ///创建实体对象
                    var list = bll.GetList<EABasic_Info>().Where(t => t.projectNumber == HttpContext.Current.Request["projectNumber"]).FirstOrDefault();
                    if (i == 1)
                    {
                        list.basic_Excl = "/File/" + hp.FileName;
                    }
                    else
                    {
                        list.basic_Enclosure = "/File/" + hp.FileName;
                    }

                    if (bll.AUpdateEntity(list.basic_id))
                    {
                        return 1;//添加成功
                    }
                    else
                    {
                        return 0;//添加失败
                    }

                }
                else
                {
                    return 2;//说明文件格式不正确
                }
            }
            else
            {
                return 3;//说明没有读取到文件流
            }
        }



        #endregion
        #endregion
    }
}
